package yxy.game.pm2.command.equip;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.equipdata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.BlacksmithMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.PackageHandle;

/** 工坊 装备合成 (手动合成，非一键合成)*/
@MessageType(proto = Command.EquipementFusion, classtype = BlacksmithMessage.EquipementFusionRequest.class)
public class CMD_EquipementFusion extends Command<BlacksmithMessage.EquipementFusionRequest> {

	@Override
	public void execute(MessageSource messageSource, BlacksmithMessage.EquipementFusionRequest request) {
		String playerCode = messageSource.getTag();
		int costId = request.getCostId();
		long number = request.getNumber();// 合成的数量

		BlacksmithMessage.EquipementFusionResponse.Builder response = BlacksmithMessage.EquipementFusionResponse.newBuilder();

		CurrencyType currencyType = CurrencyType.CT_装备;
		equipdata _equipdata = DataCenter.getData(costId, equipdata.class);
		if (_equipdata == null) {
			send(messageSource, response, Result.EquipFusionFail);// 合成失败
			return;
		}
		int targetId = _equipdata.getUpgradeid();
		if (targetId < 0) {
			send(messageSource, response, Result.EquipFusionFail);// 合成失败
			return;
		}
		if (number < 0) {
			send(messageSource, response, Result.EquipFusionFail);// 合成失败
			return;
		}
		try {
			long hasNumber = Currency.number(playerCode, currencyType, costId,false);
			if (number > hasNumber) { //判断数量
				send(messageSource, response, Result.EquipFusionFail);// 合成失败
				return;
			}

			Currency 铜钱消耗 = Currency.create(CurrencyType.CT_钱币, 0, _equipdata.getUpcost() * number);
			Currency 装备消耗 = Currency.create(CurrencyType.CT_装备, costId, _equipdata.getHeavenum() * number);

			new CurrencyHandle().cost(playerCode, CCReason.工坊, 铜钱消耗, 装备消耗);

			Currency gain = Currency.create(currencyType, targetId, number);
			new CurrencyHandle().addCurrency(playerCode, CCReason.工坊, gain);

			PackageMessage.Item.Builder item = PackageMessage.Item.newBuilder();
			item.setDataId(targetId);
			item.setNumber(number);
			response.setTarget(item);

			PackageMessage.Package.Builder pack = new PackageHandle().getPackage(playerCode, currencyType);
			response.setPack(pack);
			send(messageSource, response, Result.Success);
		} catch (CurrencyNotEnoughException e) {
			send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
			return;
		} catch (Exception e) {
			send(messageSource, response, Result.DataError);// 合成失败
			return;
		}

	}

	private void send(MessageSource messageSource, BlacksmithMessage.EquipementFusionResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}

	public static void main (String[] args) {
		int num = Integer.MAX_VALUE + 1;
		System.out.println(num);
		if (num < 0) {
			System.out.println("error");
		}
	}
}